#!/usr/bin/python
# -*- coding: UTF-8 -*-

from selenium import webdriver;
from bs4 import BeautifulSoup

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

import stock.anlayse
from stock.iwen_anlayse import StockAlayse
option = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups': 0, 'download.default_directory': 'E:\\Code\\pythonCode\\shizhan\\stock'}
option.add_experimental_option('prefs', prefs)
# option.add_argument('headless')
browser=webdriver.Chrome(options=option)
# browser.set_window_size(1400,900)
wait=WebDriverWait(browser, 10)

stock_anlayse= StockAlayse()


user_name=18086107575
password=441432




def search():
    url="http://www.iwencai.com/?allow_redirect=false"
    browser.get(url)
    input = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#auto"))
    )

    click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#qs-enter')))
    input.clear()
    search_content="所属行业,最近一周收盘价创历史新高,或者创一年新高.或者半年新高,最近一年研报数量>0";
    # search_content="2013年到2015年6月8日涨幅前100名"
    input.send_keys(search_content)
    click.click()
    content = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".stockpick_main"))
    )
    html=browser.page_source
    soup = BeautifulSoup(html, 'lxml')
    item=soup.select(".page_bar .total")

    total_page=0
    if item:
        total_page=item[0].get_text()
    return total_page


def next_page(number):
    print("current page:"+str(number))
    click = wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, '#next')))
    click.click()
    wait.until(
        EC.text_to_be_present_in_element(
            (By.CSS_SELECTOR, '.page_bar .current'), str(number)
        )
    )


def get_stock_code():
    content = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, ".stockpick_main"))
    )
    html=browser.page_source
    soup = BeautifulSoup(html, 'lxml')
    items=soup.select("#tableWrap .static_con_outer  .static_table > tbody > tr")
    for item in items:
        code=item.select(".item .em")[0].get_text()
        name = item.select(".item .em")[1].get_text()
        print(code,name)
        # write_stock_code(code)
    # tableWrap > div.table_templ_box.fixed_atomic_table_con > div > div.static_con_outer > div > table

def write_stock_code(code):
    with open("stock_2016.txt","a",encoding="utf-8") as f:
        f.write(code)
        f.write("\n")

def run():
    main()

def main():
    total_page=search()
    print(total_page)
    login()
    stock_anlayse.get_stock_code()

    # browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    # result=re.compile(r"\d+").search(total_page).group(0)
    # for i in range(2,int(result)+1):
    #     next_page(i)


def exportFile():
    print("exprotfile")
    click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#table_top_bar > table > tbody > tr > td.left_col > div.action_group > ul > li.export')))
    click.click()

def login():
    print("login")
    click = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#top_left_bar > a.login.left_sub_elem')))
    click.click()
    time.sleep(4)

    browser.switch_to.frame("login_iframe")

    username = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "body > div.loginCon > div.accountLogin > form > label:nth-child(6) > input.account"))
    )

    password = wait.until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "body > div.loginCon > div.accountLogin > form > label:nth-child(7) > input.password"))
    )
    username.send_keys("18086107575")
    password.send_keys("441432")
    login = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > div.loginCon > div.accountLogin > div > span')));
    login.click()
    time.sleep(3)

    browser.switch_to.default_content()
    time.sleep(2)
    closeTip()
    time.sleep(1)
    exportFile()
    time.sleep(8)


def closeTip():
    close=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'body > div.popup_win.hidden.popup_tip_orange.thead_drag_tip.popup_tip > div > span')));
    close.click()




if __name__ == '__main__':
    main()
    # stock_anlayse.get_stock_code()
